let app = getApp()
import detailCanvas from '../../utils/shareCanvas.js'; 
var stylecss = require('../../utils/stylecss.js');
Component({
    properties: {
        changeuser: {
          type: Array,
          observer: function(newVal, oldVal) {
            this.updatuser()
          }
        },
        defaultVideoList: {
          type: Array,
          observer: function(newVal, oldVal) {
            this.updateRate()
          }
        }
      },
      data:{
        showcompanyname:0,
        saveText:'保存到相册，分享给好友',
        curIndex:0,
        txVideo:1,
        tabbar_index:0,
        loadingText: "",
        scrollPrompt:wx.getStorageSync('scrollPrompt'),
        siteDomain:app.globalData.siteDomain,
        play:true,
        pageLoad: 1,
        goodsPreview: 1,
        controlsFlag: {
            showCenterPlayBtn: !1,
            controls: !1,
            showFullscreenBtn: !1,
            showPlayBtn: !1,
            enableProgressFesture: !0,
            autoplay: !1,
            loop: !0
        },
      },

    lifetimes: {
      ready:function(){
          wx.getSystemInfo({
            success: (res)=>{
              this.setData({
                screen_width: res.windowWidth/375,
                screen_height: res.windowHeight
             })
            },
          });
        },
        attached: function () {
            var that=this
            wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'Video_getVideo',
              },
              success: function(res) {
                  that.setData({
                    rightIcon:res.data.site_config,
                    showcompanyname:res.data.site_config.company_name
                })
                if(app.globalData.showcompanyname==1){
                  that.setData({
                    showcompanyname:0
                  })
                }
                wx.setStorageSync('float',res.data.info.common.float);
                wx.setStorageSync('site_config',res.data.site_config);
              }
            })
            if(app.globalData.showcompanyname!=1){
              setTimeout(function() {
                that.setData({
                  showcompanyname:0
                })
              }, 2000);
              app.globalData.showcompanyname=1
            }
        
            wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'user_busStaff',
                staff_id:wx.getStorageSync('staff_bind_id'),
                user_id:wx.getStorageSync('user_id')
              },
              success: function(res) {
                 wx.setStorageSync('staff_bind_id',res.data.info.staff_id);
                  that.setData({  
                    userInfo:res.data.info,
                    avatar:typeof(res.data.info)!='undefined'?res.data.info.staff_avatar:''
                  })
              }
            })

            wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'Staff_getStaffInfo',
                staff_id:wx.getStorageSync('staff_bind_id'),
                user_id:wx.getStorageSync('user_id')
              },
              success: function(res) {
                  that.setData({  
                    is_follow:typeof(res.data.info)!='undefined'?res.data.info.is_follow:''
                  })
              }
            })

            wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'Site_getSiteAbout'
              },
              success: function(res) {
                that.setData({
                  business:res.data.info
               })
              }
            })
            setTimeout(function() {
                that.setData({
                    goodsPreview:!1
                 })
              }
              ,10000)
       }
      },
    methods: {
      
      updatuser:function(){
        var that=this
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'user_busStaff',
            staff_id:wx.getStorageSync('staff_bind_id'),
            user_id:wx.getStorageSync('user_id')
          },
          success: function(res) {
             wx.setStorageSync('staff_bind_id',res.data.info.staff_id);
              that.setData({  
                userInfo:res.data.info,
                avatar:typeof(res.data.info)!='undefined'?res.data.info.staff_avatar:''
              })
              wx.request({
                url: app.globalData.siteroot,
                data: {
                  do:'Staff_getStaffInfo',
                  staff_id:res.data.info.staff_id,
                  user_id:wx.getStorageSync('user_id')
                },
                success: function(res) {
                    that.setData({  
                      is_follow:typeof(res.data.info)!='undefined'?res.data.info.is_follow:''
                    })
                }
              })
          }
        })

      

        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Site_getSiteAbout'
          },
          success: function(res) {
            that.setData({
              business:res.data.info
           })
          }
        })
      },
      companyname:function(){
        this.setData({
          showcompanyname:0
        })
      },
      cancelhaibao:function(){
        this.setData({
          shareDialogPosterVisible:false,
          shareDialogPosterMask:false
        })
      },
     sharehaibao:function(){
        wx.showLoading({
          title: '绘制中',
          mask: true,
        });
        let that = this;
        wx.request({
          url: app.globalData.siteroot,
          data: {
            do:'Qrcode_getQrCode',
            path:'yb_shopv2/entry/video-market/index?pid=0&id='+that.data.share_id+'&sid=99'
          },
          success: function(res) {
               that.setData({
                codeurl:res.data.qrurl
               })
               console.log(res)
               let title =that.data.share_title;
               let detailImg =app.globalData.siteDomain+'/addons/yb_shopv2_video/core/head_convert.php?url='+that.data.share_cover;
               let content = that.properties.content;
               let action = that.properties.action;
               let codeImg =res.data.qrurl;
               var user=wx.getStorageSync('userinfo')
               let headUrl = app.globalData.siteDomain+'/addons/yb_shopv2_video/core/head_convert.php?url='+user.user_headimg;
               Promise.all([
                that.getCode(detailImg),
                that.getCode(codeImg),
                that.getCode(headUrl)
               ]).then(res=>{
                   let sWidth = that.data.screen_width;
                   let sHeight = that.data.screen_height;
                   let code = res[1].tempFilePath;
                   let img = res[0].tempFilePath;
                   let headimg = res[2].tempFilePath;
                   detailCanvas(that,sWidth,sHeight,headimg,img,code,title,action,content,function(res){
                   wx.hideLoading();
                   that.setData({
                     posters:res
                   });
                 });
               })
          }
        })
            this.setData({
              shareDialogVisible:false,
              shareDialogMask:false,
              shareDialogPosterVisible:true,
              shareDialogPosterMask:true
            })
        },
        saveShareImg: function () {
         let posters = this.data.posters;
         wx.saveImageToPhotosAlbum({
          filePath: posters,
          success: (res)=>{
            wx.showToast({
              title:'保存成功',
              icon:'success'
            })
            this.setData({
              posters:'',
              shareDialogPosterVisible:false,
              shareDialogPosterMask:false
            })
          },
          fail: (err)=>{
            wx.showToast({
              title: '请授权保存图片',
              icon: 'none',
            });
            this.setData({
              isShare:true
            })
          },
        })
          
        },
        getCode(img){
          return new Promise((resolve,reject)=>{
            wx.downloadFile({
              url: img,
              success: (res) => {
                resolve(res)
              }
            })
          })
        },
        userFocus:function(){
          var that=this
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Staff_follow',
              staff_id:wx.getStorageSync('staff_bind_id'),
              user_id:wx.getStorageSync('user_id')
            },
            success: function(res) {
              wx.request({
                url: app.globalData.siteroot,
                data: {
                  do:'Staff_getStaffInfo',
                  staff_id:wx.getStorageSync('staff_bind_id')
                },
                success: function(res) {
                  that.setData({
                    info:res.data.info,
                    is_follow:1
                  })
                }
              })
            }
          })
        },
        bussinessinfo:function(){
              wx.navigateTo({
                url: '/yb_shopv2/pages/videoMine/videoMine',
              })
        },
        closegoodsPreview:function(){
             this.setData({
                goodsPreview:!1
             })
        },
        cantact:function(){
          let curIndex=this.data.curIndex;
          var id=this.data.videoList[curIndex].id;
          var that=this
          var newlist=that.data.videoList;
          newlist[curIndex]['wx_times']=newlist[curIndex]['wx_times']+1;
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Video_updVideoTimes',
              id:id,
              staff_id:wx.getStorageSync('staff_bind_id'),
              times_type:5,
              user_id:wx.getStorageSync('user_id')
            },
            success: function(res) {
               that.setData({
                  videoList:newlist
              })
              wx.navigateTo({
                url: '/yb_shopv2/native/all/index?type=chat'
              })
            }
          })
        },
        onShareAppMessage: function(e){
            let curIndex=this.data.curIndex;
            var title=this.data.videoList[curIndex].title;
            var id=this.data.videoList[curIndex].id;
            var that=this
            var newlist=that.data.videoList;
            newlist[curIndex]['share_times']=newlist[curIndex]['share_times']+1;
            wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'Video_updVideoTimes',
                id:id,
                staff_id:wx.getStorageSync('staff_bind_id'),
                times_type:2,
                user_id:wx.getStorageSync('user_id')
              },
              success: function(res) {
                that.setData({
                  videoList:newlist
               })
              }
            })
            if (e.from === 'button') {
                console.log(e.target)
              }
         
        },
        cancelshare:function(){
            this.setData({
                shareDialogVisible:false,
                shareDialogMask:false
               })
        },
        btnshare:function(e){
           this.setData({
            shareDialogVisible:true,
            shareDialogMask:true
           })
           let curIndex=this.data.curIndex;
           this.triggerEvent('sharefriend',this.data.videoList[curIndex])
        },
        btnTelPhone:function(e){ 
            var that=this
            const  id=e.currentTarget.dataset.id;
            const  index=e.currentTarget.dataset.index;
            var newlist=that.data.videoList;
            newlist[index]['phone_times']=newlist[index]['phone_times']+1;
            var phone =e.currentTarget.dataset.key;
            wx.makePhoneCall({
              phoneNumber:phone,
              success:function(){
                wx.request({
                  url: app.globalData.siteroot,
                  data: {
                    do:'Video_updVideoTimes',
                    id:id,
                    staff_id:wx.getStorageSync('staff_bind_id'),
                    times_type:4,
                    user_id:wx.getStorageSync('user_id')
                  },
                  success: function(res) {
                    that.setData({
                      videoList:newlist
                   })
                  }
                })
               
              },
              fail:function(){
                console.log('拨打失败')
              }
            })
          },
        videoplay:function(e){
            let index=e.currentTarget.dataset.index;
            let curIndex=this.data.curIndex;
            var id=this.data.videoList[curIndex].id;
            let videoCtx = wx.createVideoContext('video_'+id+index, this)
            if (this.data.play) {
                videoCtx.pause()
                this.setData({
                    play: false
                })
            }
            else
            {
                videoCtx.play();
                this.setData({
                    play:true
                })
            }
        },
        videochage:function(e){
            let curIndex = e.detail.current;
            var that=this
          
            if(curIndex>this.data.curIndex)
            {
                let curIndex2 = e.detail.current-1;
                var id=this.data.videoList[curIndex2].id;
                let videoCtx2 = wx.createVideoContext('video_'+id+curIndex2, this)
                videoCtx2.pause();
            }
            else
            {
                let curIndex2 = e.detail.current+1;
                var id=this.data.videoList[curIndex2].id;
                let videoCtx2 = wx.createVideoContext('video_'+id+curIndex2, this)
                videoCtx2.pause();
            }
            this.setData({
                curIndex:curIndex
            })
            this.setData({
              share_cover:that.data.videoList[curIndex].cover,
              share_title:that.data.videoList[curIndex].title,
              share_id:that.data.videoList[curIndex].id
            })
            var id=this.data.videoList[curIndex].id;
            this.updVideoTimes(id)
            let videoCtx = wx.createVideoContext('video_'+id+curIndex, this)
            videoCtx.play();
            this.setData({
                play:true
            })
            this.triggerEvent('sharefriend',this.data.videoList[curIndex]);
          },
        know:function(){
            wx.setStorageSync('scrollPrompt',1);
            this.setData({
                scrollPrompt:1
            })
        },
        advert:function(e){
          var url=JSON.parse(e.currentTarget.dataset.url)
          if(typeof(url)!='undefined')
          {
            if(url.type=='none'){
              return false;
            }
            stylecss.jumpurl(url);
          }
        },
        updVideoTimes:function(id){
          var that=this
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Video_updVideoTimes',
              id:id,
              staff_id:wx.getStorageSync('staff_bind_id'),
              times_type:1,
              user_id:wx.getStorageSync('user_id')
            },
            success: function(res) {
            }
          })
        },
        updateRate:function(){
            var that=this
            var videoList=that.data.defaultVideoList
            for(var s=1;s<videoList.length; s++){
                var realurl=videoList[s].realurl
                 wx.request({
                  url: app.globalData.siteroot,
                  data: {
                    do:'Video_realUrl',
                    type:videoList[s].type,
                    url:videoList[s].url,
                    vid:videoList[s].id
                  },
                  success: function(res) {
                    realurl=res.data.data
                  }
                })
                videoList[s].realurl=realurl
            }
            this.setData({
                videoList:videoList
            })
            this.triggerEvent('sharefriend',this.data.videoList[0]);
            if(that.data.defaultVideoList.length>0)
            {
                this.setData({
                  share_cover:that.data.videoList[0].cover,
                  share_title:that.data.videoList[0].title,
                  share_id:that.data.videoList[0].id
                })
                var id=this.data.videoList[0].id;
                this.updVideoTimes(id)
                let videoCtx = wx.createVideoContext('video_'+id+0, this)
                videoCtx.play();
            }
        }
    }
});
